package com.yjht.modules.weixin.service.impl;

import com.yjht.common.exception.LocalException;
import com.yjht.modules.weixin.dao.WxWechatRelationAreaDao;
import com.yjht.modules.weixin.entity.WxWechatRelationAreaEntity;
import com.yjht.modules.weixin.service.WxWechatRelationAreaService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yjht.common.utils.PageUtils;
import com.yjht.common.utils.Query;



@Service("wxWechatRelationAreaService")
public class WxWechatRelationAreaServiceImpl extends ServiceImpl<WxWechatRelationAreaDao, WxWechatRelationAreaEntity> implements WxWechatRelationAreaService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<WxWechatRelationAreaEntity> page = this.selectPage(
                new Query<WxWechatRelationAreaEntity>(params).getPage(),
                new EntityWrapper<WxWechatRelationAreaEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<WxWechatRelationAreaEntity> queryRelationArea(int wxid) {
        EntityWrapper<WxWechatRelationAreaEntity> ew = new EntityWrapper<>();
        WxWechatRelationAreaEntity condition = new WxWechatRelationAreaEntity();
        condition.setWechatId(wxid);
        ew.setEntity(condition);
        List<WxWechatRelationAreaEntity> list = this.selectList(ew);
        return list;
    }

    @Override
    public boolean updateOrSaveArea(int wxid, int[] areaId) {
        EntityWrapper<WxWechatRelationAreaEntity> ew = new EntityWrapper<>();
        WxWechatRelationAreaEntity condition = new WxWechatRelationAreaEntity();
        condition.setWechatId(wxid);
        ew.setEntity(condition);
        boolean deleteArea = this.delete(ew);
        if(deleteArea == false){
            return false;
        }
        if(areaId != null && areaId.length > 0){
            int length = areaId.length;
            WxWechatRelationAreaEntity entity = new WxWechatRelationAreaEntity();
            for(int i = 0;i < length;i ++){
                entity.setWechatId(wxid);
                entity.setAreaId(areaId[i]);
                boolean flag = this.insert(entity);
                if(flag == false){
                    return flag;
                }
            }
            return true;
        }else {
            throw new LocalException("0LE1101001");
        }
    }

}
